package com.hula.onion.book.server.spider.platforms.xinlianzaixian;

import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.SpiderCommon;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by work on 2018/1/25.
 */
@Component("newUnionSpider")
public class NewUnionSpider extends SpiderCommon {

    @Autowired
    private NewUnionSpiderHelper spiderHelper;

    private static final String PRE_LOGIN_URL = "https://www.newunion.cn/logining.do";


    private static final String LOGIN_URL = "https://www.newunion.cn/login.do";

    @Override
    public CommonResponse execute() {
        String str = HttpUtils.post(PRE_LOGIN_URL,
                spiderHelper.installRequestParams(loginForm), getHttpContext());
        if (StringUtils.isEmpty(str)) {
            return new CommonResponse(ResponseStatusEnum.FAIL);
        }
        if ("3".equals(str)) {
            return new CommonResponse(ResponseStatusEnum.ACCOUNT_ERROR);
        }
        if ("1".equals(str)) {
            String loginStr = HttpUtils.post(LOGIN_URL, null, getHttpContext());
            if (StringUtils.isEmpty(loginStr)) {
                return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
            }
            return spiderHelper.parseAccountBalance(loginStr);
        }
        return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
    }
}
